/*
Copyright 2011 Gabriel Flores

*/
package com.asfusion.mate.extension.actions.builders
{
	import com.asfusion.mate.actionLists.IScope;
	import com.asfusion.mate.actions.IAction;
	import com.asfusion.mate.actions.builders.ServiceInvoker;
	import com.asfusion.mate.core.ISmartObject;
	
	import mx.messaging.ChannelSet;
	import mx.messaging.config.ServerConfig;
	import mx.rpc.AsyncToken;

	/**
	 * ServiceInvoker is the base class for the following channel set invokers:
	 * <ul><li>ChannelSetLoginInvoker</li>
	 * <li>ChannelSetLogoutInvoker</li></ul>
	 * 
	 * @see com.asfusion.mate.actions.builders.ChannelSetLoginInvoker
	 * @see com.asfusion.mate.actions.builders.ChannelSetLogoutInvoker
	 */
	public class ChannelSetInvoker extends ServiceInvoker implements IAction
	{
		
		/*-.........................................channelSetName..........................................*/
		private var _channelSetName:String;
		/**
		 * Lets you specify a channelSetName value on the client; not supported for destinations that use the JavaAdapter. 
		 * This allows you to provide more than one channelSetName that can be accessed from a single destination on the server.
		 * 
		 */
		public function get channelSetName():String
		{
			return _channelSetName;
		}
		public function set channelSetName(value:String):void
		{
			_channelSetName = value;
		}
		
		/*-.........................................instance..........................................*/
		/**
		 * If this property is null, a new RemoteObject instance is created on 
		 * the <code>prepare</code> method. Otherwise, this instance will be used.
		 * The class that will be used to create the instance if none is provided is
		 *   <code>mx.rpc.remoting.mxml.RemoteObject</code>
		 * 
		 * @default null
		 */
		public function get instance():ChannelSet
		{
			return currentInstance;
		}
		public function set instance(value:ChannelSet):void
		{
			currentInstance = value;
		}

		
		/*-----------------------------------------------------------------------------------------------------------
		*                                          Override protected methods
		-------------------------------------------------------------------------------------------------------------*/
		/*-.........................................createInstance..........................................*/
		/**
		 * @inheritDoc
		 */
		override protected function prepare(scope:IScope):void
		{
			super.prepare(scope);
				
			if(!currentInstance)
			{
				if(channelSet) 
				{
					currentInstance = channelSet;
				} else {
					currentInstance = ServerConfig.getChannelSet(channelSetName);
				}
			}
	
		}
		
		/*-.........................................run..........................................*/
		/**
		 * @inheritDoc
		 */
		override protected function run(scope:IScope):void
		{
			var currentChannelSet:ChannelSet = ( currentInstance as ChannelSet );
			
			token = invoke( currentChannelSet ); 
			
			scope.lastReturn = token;
		}
		
		/*-.........................................invoke..........................................*/
		/**
		 * this is an abstract method that is override by 
		 * <ul><li>ChannelSetLoginInvoker</li>
		 * <li>ChannelSetLogoutInvoker</li></ul>
		 * 
		 * @see com.asfusion.mate.actions.builders.ChannelSetLoginInvoker
		 * @see com.asfusion.mate.actions.builders.ChannelSetLogoutInvoker
		 */
		protected function invoke( channelSet:ChannelSet ):AsyncToken
		{
			return null;
		}
		
	}
}